﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using trangbaocao.Models;
using System.Data;
using System.Data.OracleClient;
using Microsoft.Reporting.WebForms;

namespace trangbaocao.Reports.Manage_Powers
{
    public partial class Report_Device_Powers_Configuration : System.Web.UI.Page
    {
        StationsModel StationsModel = new StationsModel();
        Main_StationsModel Main_StationsModel = new Main_StationsModel();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session != null && Convert.ToInt32(Session["quyen_id"]) > 2)
                {
                    Radio1.Visible = false;
                }
                List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
                List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(Convert.ToInt32(main_StationsModel[0].id));
                cbtrungtam.DataSource = main_StationsModel;
                cbtrungtam.DataTextField = "name";
                cbtrungtam.DataValueField = "id";
                cbtrungtam.DataBind();

                cbtram.DataSource = stationModel;
                cbtram.DataTextField = "name";
                cbtram.DataValueField = "id";
                cbtram.DataBind();

                //cbnam.DataSource = getYear();
                //cbnam.DataTextField = "nam";
                //cbnam.DataValueField = "nam";
                //cbnam.DataBind();

                //for (int i = 1; i <= 12; i++)
                //{
                //    cbthang.Items.Add(i.ToString());
                //    cbthang2.Items.Add(i.ToString());
                //}
                ////cbthang.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
                //cbthang2.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
            }
        }

        protected void Radio1_CheckedChanged(object sender, EventArgs e)
        {
            if (Radio1.Checked)
            {
                cbtrungtam.Enabled = false;
                cbtram.Enabled = false;
            }
            if (Radio2.Checked)
            {
                cbtrungtam.Enabled = true;
                cbtram.Enabled = true;
            }
        }

        protected void radio_tram_group_CheckedChanged(object sender, EventArgs e)
        {
            if (radio_tram_group.Checked)
            {
                cbtram.Enabled = false;
            }
            if (radio_tram.Checked)
            {
                cbtram.Enabled = true;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ReportViewer1.Reset();
            Main_Sort_DeviceModel Main_Sort_DeviceModel = new Main_Sort_DeviceModel();
            List<Main_Sort_DeviceModel> main_Sort_DeviceModel = Main_Sort_DeviceModel.getChungloaithietbi();

            ReportViewer1.LocalReport.ReportPath = "Reports/Manage_Powers/Device_Power_Configuration.rdlc";
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("dts_device_power_ds", getDslamConfiguration().Tables[0]));

            ReportViewer1.DataBind();
            ReportViewer1.LocalReport.Refresh();
        }

        protected void cbtrungtam_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(Convert.ToInt32(cbtrungtam.SelectedValue));
            cbtram.DataSource = stationModel;
            cbtram.DataTextField = "name";
            cbtram.DataValueField = "id";
            cbtram.DataBind();
        }
        public DataSet getDslamConfiguration()
        {
            string sql = "";
            //string nam = cbnam.SelectedValue;
            //string thang = cbthang.SelectedValue;
            //string thang2 = cbthang2.SelectedValue;
            string trung_tam_id = cbtrungtam.SelectedValue;
            string tram_id = cbtram.SelectedValue;

            string sql_tram = "";
            if (radio_tram.Checked)
                sql_tram = " and d.id='" + tram_id + "'";
            if (Radio2.Checked)
            {
                //sql = " select a.id,b.id device_stations_id,b.ma_he_thong,b.name,c.name ten_tram,c.dia_chi,d.name ten_trung_tam,";
                //sql += " a.ngay_hd,a.thoi_gian_hd,a.ngay_cap_nhat,a.user_cap_nhat,a.ghi_chu,a.thang,a.nam,c.id stations_id,d.id main_stations_id";
                //sql += " from fuel_detail a,device_stations b,stations c, main_stations d where a.main_stations_id=d.id and a.stations_id=c.id ";
                //sql += " and a.device_stations_id=b.id and a.thang=" + thang + " and a.nam=" + nam + " and d.id=" + trung_tam_id + sql_tram + " order by d.name,c.name,b.name,a.ngay_hd";

                sql += "select  a.device_stations_id,a.ma_he_thong ma_thiet_bi,e.name ten_trung_tam,d.name ten_tram, a.name_parameter ten_tham_so,b.name ten_thiet_bi,c.name loai_tb,";
                sql += " a.value_lapdat so_luong_lap_dat,a.value_using so_luong_su_dung,a.name_parameter ,a.unit don_vi_tinh,to_char(a.date_setup,'dd/mm/yyyy') ngay_thiet_lap,a.date_sys,a.note from";
                sql += "(select a.*,b.stations_id,b.main_stations_id,b.ma_he_thong,c.name_parameter ,c.unit  from";
                sql += " (select * from parameter_using where main_sort_device_id = 9) a,";
                sql += " (select id,stations_id,main_stations_id,ma_he_thong from device_stations) b,";
                sql += " (select id,name_parameter,unit from parameterrs_base) c";
                sql += " where a.device_stations_id=b.id and a.parameter_base_id=c.id) a,(select * from device) b";
                sql += " ,(select * from sort_device) c,(select * from stations) d,(select * from main_stations) e";
                sql += " where a.device_id=b.id";
                sql += " and a.sort_device_id=c.id and a.stations_id=d.id and a.main_stations_id=e.id and e.id=" + trung_tam_id + sql_tram;
                sql += " order by e.name,d.name,c.name,b.name,a.device_stations_id,a.unit";

            }
            if (Radio1.Checked)
            {
                sql += "select  a.device_stations_id,a.ma_he_thong ma_thiet_bi,e.name ten_trung_tam,d.name ten_tram, a.name_parameter ten_tham_so, b.name ten_thiet_bi,c.name loai_tb,";
                sql += " a.value_lapdat so_luong_lap_dat,a.value_using so_luong_su_dung,a.name_parameter ,a.unit don_vi_tinh,to_char(a.date_setup,'dd/mm/yyyy') ngay_thiet_lap,a.date_sys,a.note from";
                sql += " (select a.*,b.stations_id,b.main_stations_id,b.ma_he_thong,c.name_parameter ,c.unit  from";
                sql += " (select * from parameter_using where main_sort_device_id =9) a,";
                sql += " (select id,stations_id,main_stations_id,ma_he_thong from device_stations) b,";
                sql += " (select id,name_parameter,unit from parameterrs_base) c";
                sql += " where a.device_stations_id=b.id and a.parameter_base_id=c.id) a,(select * from device) b";
                sql += " ,(select * from sort_device) c,(select * from stations) d,(select * from main_stations) e";
                sql += " where a.device_id=b.id";
                sql += " and a.sort_device_id=c.id and a.stations_id=d.id and a.main_stations_id=e.id";
                sql += " order by e.name,d.name,c.name,b.name,a.device_stations_id,a.unit";
            }

            OracleConnection conn = General.ConnectDataBase();
            OracleCommand omd = new OracleCommand(sql, conn);
            OracleDataAdapter oda = new OracleDataAdapter(omd);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Lỗi kết nối với cơ sở dữ liệu!')</SCRIPT>");
            }
            oda.Fill(ds, "0");
            conn.Close();
            return ds;
        }
        public DataSet getYear()
        {
            string sql = "SELECT distinct nam from fuel order by nam desc";
            OracleConnection conn = General.ConnectDataBase();
            OracleCommand omd = new OracleCommand(sql, conn);
            OracleDataAdapter oda = new OracleDataAdapter(omd);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Lỗi kết nối với cơ sở dữ liệu!')</SCRIPT>");
            }
            oda.Fill(ds, "0");
            conn.Close();
            return ds;
        }
    }
}